<?php
namespace app\tools\logic;
use think\facade\Db;

class RechargeLogic{
	static function getList(){
		$page_size = input('page_size', 10);
		$item_name = input('item_name', '');
		$obj = Db::name('recharge')
				->alias('r')
				->where('r.status','<>',127);
		$item_name && $obj->where('r.item_name','like','%'.$item_name.'%');
		$objs = $obj->order('r.sort asc, r.id desc')
				->paginate($page_size);

		$objs->render();
    	$list = $objs->toArray();
		if($list && $list['data']){
			//获取会员列表
			$mls = Db::name('member_grade')
					->where('status','<>',127)
					->order('level asc')
					->select()
					->toArray();
			foreach($list['data'] as $k=>$v){
				$list['data'][$k]['grade_level'] = [];
				foreach($mls as $vv){
					$tmp = [];
					$tmp['level'] = $vv['level'];
					$tmp['grade_name'] = $vv['grade_name'];
					$tmp['gift_set'] = Db::name('recharge_level')
										->field('item_name,item_name_en,amount,gift_rate')
										->where('recharge_id',$v['id'])
										->where('grade_level', $vv['level'])
										->order('amount asc')
										->select();
					$list['data'][$k]['grade_level'][] = $tmp;
				}
			}
		}

		return $list;
	}

	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('recharge')
					->alias('r')
					->where('r.id', $id)
					->find();

		$info['grade_level'] = [];
		$mls = Db::name('member_grade')
					->where('status','<>',127)
					->order('level asc')
					->select()
					->toArray();
		foreach($mls as $vv){
			$tmp = [];
			$tmp['level'] = $vv['level'];
			$tmp['grade_name'] = $vv['grade_name'];
			$tmp['gift_set'] = Db::name('recharge_level')
								->field('item_name,item_name_en,amount,gift_rate')
								->where('recharge_id',$info['id'])
								->where('grade_level', $vv['level'])
								->order('amount asc')
								->select();
			$info['grade_level'][] = $tmp;
		}

		return $info;

	}

	static function save(){
		$id = input('id');
		if(!$id){
			return 110;
		}

		$info = Db::name('recharge')
				->field('id')
				->where('id', $id)
				->where('status','<>',127)
				->find();
		if(!$info){
			return 134;
		}

		$data = [];
		$data['item_name'] = input('item_name','');
		$data['item_name_en'] = input('item_name_en','');
		$data['extension'] = input('extension', '');
		$data['extension_en'] = input('extension_en', '');
		$data['status'] = input('status', 1);
		$data['sort'] = input('sort', 50);
		$data['content'] = input('content', '');
		$data['content_en'] = input('content_en', '');
		$data['create_time'] = date('Y-m-d H:i:s');
		$grade_level = input('grade_level', []);
		// 启动事务
		Db::startTrans();
		try {
			//活动，始终只能有一条记录是开启状态的
			if($data['status']==1){
				Db::name('recharge')->where('status',1)->update(['status'=>0]);
			}
			//编辑活动
			Db::name('recharge')->where('id', $id)->update($data);
			//删除所有的赠送设置
			Db::name('recharge_level')->where('recharge_id', $id)->delete();
			//循环赠送项
			if($grade_level){
				foreach($grade_level as $v){
					if(isset($v['gift_set']) && $v['gift_set']){
						foreach($v['gift_set'] as $vv){
							$ld = [];
							$ld['recharge_id'] = $id;
							$ld['item_name'] = $vv['item_name'];
							$ld['item_name_en'] = $vv['item_name_en'];
							$ld['amount'] = $vv['amount'];
							$ld['gift_rate'] = $vv['gift_rate'];
							$ld['grade_level'] = $v['level'];
							Db::name('recharge_level')->insert($ld);
						}
					}
				}
			}

			// 提交事务
		    Db::commit();
		    return 0;
		} catch (\Exception $e) {
		    // 回滚事务
		    // print_r($e->getMessage());
		    Db::rollback();
		    return 100000;
		}
	}

	static function add(){
		$data = [];
		$data['item_name'] = input('item_name','');
		$data['item_name_en'] = input('item_name_en','');
		$data['extension'] = input('extension', '');
		$data['extension_en'] = input('extension_en', '');
		$data['status'] = input('status', 1);
		$data['sort'] = input('sort', 50);
		$data['content'] = input('content', '');
		$data['content_en'] = input('content_en', '');
		$data['create_time'] = date('Y-m-d H:i:s');
		$grade_level = input('grade_level', []);
		// 启动事务
		Db::startTrans();
		try {
			//新增活动，始终只能有一条记录是开启状态的
			if($data['status']==1){
				Db::name('recharge')->where('status',1)->update(['status'=>0]);
			}
			//新增活动
			$id = Db::name('recharge')->insertGetId($data);
			//循环赠送项
			if($grade_level){
				foreach($grade_level as $v){
					if(isset($v['gift_set']) && $v['gift_set']){
						foreach($v['gift_set'] as $vv){
							$ld = [];
							$ld['recharge_id'] = $id;
							$ld['item_name'] = $vv['item_name'];
							$ld['item_name_en'] = $vv['item_name_en'];
							$ld['amount'] = $vv['amount'];
							$ld['gift_rate'] = $vv['gift_rate'];
							$ld['grade_level'] = $v['level'];
							Db::name('recharge_level')->insert($ld);
						}
					}
				}
			}

			// 提交事务
		    Db::commit();
		    return 0;
		} catch (\Exception $e) {
		    // 回滚事务
		    // print_r($e->getMessage());
		    Db::rollback();
		    return 100000;
		}
	}

	//删除
	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		$rs = Db::name('recharge')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}
}